﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Youshow.Ace.Domain.Models;
using Youshow.Ace.Domain.Repository;

namespace BlogSystem.Domain.AccountInfo
{
    // 实体类必须集成BaseModel类
    public class User:BaseModel<int>
    {
        public string UserName { get; set; }
        public string Password { get; set; }
        public string? Sex { get; set; } = "保密";
        public DateTime BirthDay { get; set; }
        public string PasswordQuestion { get; set; }
        public string PasswordAnswer { get; set; }
        public string? Email { get; set; }
        public string? QQ { get; set; }
        public DateTime RegistTime { get; set; }

        ///<summary>
        ///验证用户登录
        ///<param name="userRepo"></param>
        ///<returns></returns>
        ///</summary>
        // 仓储类库，是一个封装了增删改查的帮助类库
        public static async Task<User> CheckLogin(string userName,string password, IRepository<User> userRepo)
        {
            var user = await userRepo.GetAsync(u => u.UserName == userName && u.Password == password);
            return user;
        }

        //注册方法
        public void ValidateRegist()
        {
            if(this.BirthDay == null)
            {
                var defaultBirthday = "2000-01-01";
                this.BirthDay = DateTime.Parse(defaultBirthday);
            }
            Regex regexQQ = new Regex(@"^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$");
            if (!regexQQ.IsMatch(this.QQ))
            {
                throw new Exception("Emial格式不正确");
            }
            this.RegistTime = DateTime.Now;
        }
    }
}
